package com.swh.recruit.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.swh.utils.IdUtil;
import com.swh.recruit.dao.TbRecruitDao;
import com.swh.recruit.entity.TbRecruit;

/**
 * @author swh
 * @Description 职位 服务层
 * @date 2021-01-12 17:57:44
 */
@Service
public class TbRecruitService {

    @Autowired
    private TbRecruitDao tbRecruitDao;

    @Autowired
    private IdUtil idUtil;


    /**
     * 查询状态不为 0 并以创建日期降序排序，查询前 12 条记录
     */
    public List<TbRecruit> findTop14ByStateOrderByCreatetimeDesc(String state){
       return tbRecruitDao.findTop14ByStateOrderByCreatetimeDesc(state);
    }


    /**
     * 查询状态为 2 并以创建日期降序排序，查询前 4 条记录
     */
    public List<TbRecruit> findTop4ByStateOrderByCreatetimeDesc(String state) {
        return tbRecruitDao.findTop4ByStateOrderByCreatetimeDesc(state);
    }


    /**
     * 查询全部列表
     *
     * @return
     */
    public List<TbRecruit> findAll() {
        return tbRecruitDao.findAll();
    }

    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<TbRecruit> findSearch(Map whereMap, int page, int size) {
        Specification<TbRecruit> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return tbRecruitDao.findAll(specification, pageRequest);
    }

    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<TbRecruit> findSearch(Map whereMap) {
        Specification<TbRecruit> specification = createSpecification(whereMap);
        return tbRecruitDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public TbRecruit findById(String id) {
        return tbRecruitDao.findById(id).get();
    }

    /**
     * 增加
     *
     * @param tbRecruit
     */
    public void add(TbRecruit tbRecruit) {
        tbRecruit.setId(idUtil.nextId() + "");
        tbRecruitDao.save(tbRecruit);
    }

    /**
     * 修改
     *
     * @param tbRecruit
     */
    public void update(TbRecruit tbRecruit) {
        tbRecruitDao.save(tbRecruit);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        tbRecruitDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<TbRecruit> createSpecification(Map searchMap) {

        return new Specification<TbRecruit>() {

            @Override
            public Predicate toPredicate(Root<TbRecruit> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                //职位名称
                if (searchMap.get("jobname") != null && !"".equals(searchMap.get("jobname"))) {
                    predicateList.add(cb.like(root.get("jobname").as(String.class), "%" + (String) searchMap.get("jobname") + "%"));
                }
                //薪资范围
                if (searchMap.get("salary") != null && !"".equals(searchMap.get("salary"))) {
                    predicateList.add(cb.like(root.get("salary").as(String.class), "%" + (String) searchMap.get("salary") + "%"));
                }
                //经验要求
                if (searchMap.get("experience") != null && !"".equals(searchMap.get("experience"))) {
                    predicateList.add(cb.like(root.get("experience").as(String.class), "%" + (String) searchMap.get("experience") + "%"));
                }
                //学历要求
                if (searchMap.get("education") != null && !"".equals(searchMap.get("education"))) {
                    predicateList.add(cb.like(root.get("education").as(String.class), "%" + (String) searchMap.get("education") + "%"));
                }
                //任职方式
                if (searchMap.get("type") != null && !"".equals(searchMap.get("type"))) {
                    predicateList.add(cb.like(root.get("type").as(String.class), "%" + (String) searchMap.get("type") + "%"));
                }
                //办公地址
                if (searchMap.get("address") != null && !"".equals(searchMap.get("address"))) {
                    predicateList.add(cb.like(root.get("address").as(String.class), "%" + (String) searchMap.get("address") + "%"));
                }
                //企业ID
                if (searchMap.get("eid") != null && !"".equals(searchMap.get("eid"))) {
                    predicateList.add(cb.like(root.get("eid").as(String.class), "%" + (String) searchMap.get("eid") + "%"));
                }
                //创建日期
                if (searchMap.get("createtime") != null && !"".equals(searchMap.get("createtime"))) {
                    predicateList.add(cb.like(root.get("createtime").as(String.class), "%" + (String) searchMap.get("createtime") + "%"));
                }
                //状态
                if (searchMap.get("state") != null && !"".equals(searchMap.get("state"))) {
                    predicateList.add(cb.like(root.get("state").as(String.class), "%" + (String) searchMap.get("state") + "%"));
                }
                //网址
                if (searchMap.get("url") != null && !"".equals(searchMap.get("url"))) {
                    predicateList.add(cb.like(root.get("url").as(String.class), "%" + (String) searchMap.get("url") + "%"));
                }
                //标签
                if (searchMap.get("label") != null && !"".equals(searchMap.get("label"))) {
                    predicateList.add(cb.like(root.get("label").as(String.class), "%" + (String) searchMap.get("label") + "%"));
                }
                //职位描述
                if (searchMap.get("content1") != null && !"".equals(searchMap.get("content1"))) {
                    predicateList.add(cb.like(root.get("content1").as(String.class), "%" + (String) searchMap.get("content1") + "%"));
                }
                //职位要求
                if (searchMap.get("content2") != null && !"".equals(searchMap.get("content2"))) {
                    predicateList.add(cb.like(root.get("content2").as(String.class), "%" + (String) searchMap.get("content2") + "%"));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

}
